import * as yaml from 'js-yaml';
import { readFileSync } from 'fs';
import { resolve } from 'path';

/**
 * @description 读取yaml配置文件
 * @exports
 * @param {string} path
 * @return {*}  {Record<string, any>}
 */
export function readYaml(path: string): Record<string, any> {
  try {
    const file = readFileSync(path, 'utf-8');
    const data = yaml.load(file);
    return data;
  } catch (err) {
    // console.error(`yaml文件[${path}]解析异常!`, err);
  }
}

/**
 * @description 获取端口
 * @export
 * @return {*}  {number}
 */
export function getPort(): number {
  const config = readYaml(resolve(__dirname, '../../configs/appservice.yaml'));
  if (config) {
    return config.app.port;
  }
  return 3000;
}
